package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyConfigInfoQuery;
import com.zl.tby.model.TbyConfigInfo;
import com.zl.tby.service.ITbyConfigInfoService;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;
import org.noear.wood.utils.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 系统参数控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/config")
public class TbyConfigInfoController {

    @Inject
    private ITbyConfigInfoService tbyConfigInfoService;

    /**
     * 保存或新增
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyConfigInfo> saveOrUpdate(@Validated TbyConfigInfo entity) {
        try {
            if (entity.getId() == null) {
                return tbyConfigInfoService.add(entity);
            }
            return tbyConfigInfoService.update(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return this.tbyConfigInfoService.delete(id);
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyConfigInfo> get(@NotNull Long id) {
        TbyConfigInfo entity = this.tbyConfigInfoService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyConfigInfoQuery query) {
        return R.ok(this.tbyConfigInfoService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyConfigInfo>> List(@Body TbyConfigInfoQuery query) {
        return R.ok(this.tbyConfigInfoService.list(query));
    }

    /**
     * 验证设备编码是否存在
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/exist", method = MethodType.GET)
    public R<Boolean> exist(@Body TbyConfigInfoQuery query) {
        return this.tbyConfigInfoService.checkCode(query.getId(), query.getCode());
    }

    /**
     * 根据编码获取配置，多个编码逗号分隔
     * @param code: 编码
     * @return 结果
     */
    @Mapping(value = "/code", method = MethodType.GET)
    public R<List<OptionType>> getByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return R.ok(new ArrayList<>());
        }
        TbyConfigInfoQuery query = new TbyConfigInfoQuery();
        query.setCodeList(Arrays.asList(ToolsUtil.splitStr(code, ',')));
        List<TbyConfigInfo> dbList = this.tbyConfigInfoService.list(query);
        List<OptionType> rsList = new ArrayList<>();
        for (TbyConfigInfo config : dbList) {
            rsList.add(new OptionType(config.getCode(), config.getContent()));
        }
        return R.ok(rsList);
    }

}
